package com.sun.showcase.controller.app;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.showcase.biz.mall.service.AdvertService;
import com.sun.showcase.biz.mall.service.HomeGoodsService;
import com.sun.showcase.client.query.mall.AdvertQuery;
import com.sun.showcase.client.query.mall.HomeGoodsQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.StringConstant;

@CrossOrigin
@Controller("appHomeController")
@RequestMapping(value="/app/") 
public class HomeController{
	
	@Autowired
	private HomeGoodsService homeGoodsService;
	@Autowired
	private AdvertService advertService;
	
	private Result result = new Result();
	
	/**
	 * 全部数据HomeGoods
	 */
	@ResponseBody
	@RequestMapping(value="home")
	public Result home() {
		Map<String,Object> res = new HashMap<String,Object>();
		DataGrid dg = null;
		//首页顶部banner 最多显示五个
		AdvertQuery advertQuery = new AdvertQuery();
		advertQuery.setLocation(StringConstant.ZERO);
		advertQuery.setOrderColum("order_num");
		advertQuery.setOrderRule("ASC");
		advertQuery.setPage(1L);
		advertQuery.setRows(100L);
		dg = advertService.datagrid(advertQuery);
		res.put("banner", dg.getRows());
		//推荐新品
		HomeGoodsQuery homeGoodsQuery = new HomeGoodsQuery();
		homeGoodsQuery.setRows(6L);
		res.put("newList", homeGoodsService.newList(homeGoodsQuery).getRows());
		//推荐热卖
		homeGoodsQuery.setRows(100L);
		res.put("hotList",  homeGoodsService.hotList(homeGoodsQuery).getRows());
		result.setSuccess(true);
		result.setData(res);
		return result;
	}
	
	/**
	 * 全部数据HomeGoods
	 */
	@ResponseBody
	@RequestMapping(value="hotList")
	public Result hotList(HomeGoodsQuery homeGoodsQuery) {
		result.setSuccess(true);
		result.setData(homeGoodsService.hotList(homeGoodsQuery));
		return result;
	}
	/**
	 * 全部数据HomeGoods
	 */
	@ResponseBody
	@RequestMapping(value="newList")
	public Result newList(HomeGoodsQuery homeGoodsQuery) {
		result.setSuccess(true);
		result.setData(homeGoodsService.newList(homeGoodsQuery));
		return result;
	}
	
	@RequestMapping(value = {"/web"})
	public String web(String url,ModelMap map,HttpServletResponse response){
		response.setContentType("text/javascript");//如果不这样设置 chrome浏览器无法调用
		map.put("url", url);
		return "web";
	}
}
